#pragma once

#include "..\\headers.default.h"

namespace voc_pascal
{
	class CClassData
	{
	public:
		string Name;
		int Label;
		int R,G,B;

		bool operator< (const CClassData& b)
		{
			return Label<b.Label;
		}
		bool operator== (const CClassData& b)
		{
			return Label==b.Label;
		}

		void InputFromStream(istream& inStream)
		{
			inStream>>Name>>Label>>R>>G>>B;
			CDataStructureTools::ToLowerCase(Name);
		}
		void OuputToStream(ostream& outStream)
		{
			outStream<<setw(10)<<Name<<'\t';
			outStream<<setw(3)<<Label<<'\t';
			outStream<<setw(3)<<R<<'\t';
			outStream<<setw(3)<<G<<'\t';
			outStream<<setw(3)<<B<<endl;
		}
	};
	class CClassSet
	{
	public:
		int ClassAmount;
		vector<CClassData> rgClassInfoData;
		
	public:
		void myInit(string strFN_ClassInfoData)
		{
			int i;

			ifstream inFile(strFN_ClassInfoData.c_str());

			inFile>>ClassAmount;
			rgClassInfoData.resize(ClassAmount);
			for ( i = 0; i < ClassAmount; ++i )
				rgClassInfoData[i].InputFromStream(inFile);
			inFile.clear();
			inFile.close();

			sort(rgClassInfoData.begin(), rgClassInfoData.end());
		}
	};

	class CClassInfo
	{
	public:
		CClassSet* pParentSet;
		int Index;

	public:
		CClassInfo()
		{
			pParentSet = NULL;
			Index = -1;
		}
		void myInit_ByIndex(CClassSet* pParentSet, int Index)
		{
			this->pParentSet = pParentSet;
			this->Index = Index;
		}
		void myInit_ByLabel(CClassSet* pParentSet, int Label)
		{
			int i;

			this->pParentSet = pParentSet;

			for ( i = 0; i < pParentSet->ClassAmount; ++i )
				if (pParentSet->rgClassInfoData[i].Label == Label)
					break;
			if (i >= pParentSet->ClassAmount)
				Index = -1;
			else
				Index = i;
		}
		void myInit_ByName(CClassSet* pParentSet, const string& Name)
		{
			int i;
			string ttt;

			this->pParentSet = pParentSet;
			ttt = Name;
			CDataStructureTools::ToLowerCase(ttt);
			for ( i = 0; i < pParentSet->ClassAmount; ++i )
				if (pParentSet->rgClassInfoData[i].Name == ttt)
					break;
			if (i >= pParentSet->ClassAmount)
				Index = -1;
			else
				Index = i;
		}
		void myInit_ByName(CClassSet* pParentSet, const char* Name)
		{
			int i;
			string ttt;

			this->pParentSet = pParentSet;
			ttt = Name;
			CDataStructureTools::ToLowerCase(ttt);
			for ( i = 0; i < pParentSet->ClassAmount; ++i )
				if (pParentSet->rgClassInfoData[i].Name == ttt)
					break;
			if (i >= pParentSet->ClassAmount)
				Index = -1;
			else
				Index = i;
		}
		void myInit_ByRGB(CClassSet* pParentSet, int R, int G, int B)
		{
			int i,t,tt;

			this->pParentSet = pParentSet;
			Index = -1;
			for ( i = 0; i < pParentSet->ClassAmount; ++i )
			{
				CClassData& CurrentData = pParentSet->rgClassInfoData[i];
				tt = 0;
				tt += CMyMath::sqr(CurrentData.R - R);
				tt += CMyMath::sqr(CurrentData.G - G);
				tt += CMyMath::sqr(CurrentData.B - B);
				if (Index<0 || t>tt)
				{
					Index = i;
					t = tt;
				}
			}
		}
		CClassData& GetData()
		{
			return pParentSet->rgClassInfoData[Index];
		}
		const CClassData& GetData() const
		{
			return pParentSet->rgClassInfoData[Index];
		}
	};
}

